<template>
  <div>
    <el-card>
      <!--搜索与添加区域-->
      <el-row :gutter="20"
        style="margin-bottom: 20px">
        <el-col :span="16">
          <el-button type="primary"
            @click="handleAddGroup()"
            style="float: left">新建人群</el-button>
        </el-col>
        <el-col :span="8">
          <el-input placeholder="请输入人群名称"
            v-model="queryInfo.group_name"
            clearable
            @clear="getGroupList">
            <el-button slot="append"
              icon="el-icon-search"
              @click="getGroupList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="groupList"
        border
        stripe>
        <!--        <el-table-column type="index">-->
        <!--        </el-table-column>-->
        <el-table-column prop="group_id"
          label="人群id"></el-table-column>
        <el-table-column prop="group_name"
          label="人群名称"></el-table-column>
        <el-table-column prop="creater"
          label="创建人"></el-table-column>
        <el-table-column prop="create_time"
          label="创建时间"></el-table-column>
        <el-table-column prop="reach_count"
          label="触达人数"></el-table-column>
        <el-table-column prop="is_online"
          label="是否下线"></el-table-column>
        <el-table-column label="操作"
          width="140">
          <template slot-scope="scope">
            <el-tooltip effect="dark"
              content="编辑"
              placement="top"
              :enterable="false">
              <el-button type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="handleAddGroup(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark"
              content="删除"
              placement="top"
              :enterable="false">
              <el-button type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeGroupById(scope.row.group_id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <!-- <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination> -->
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        group_name: '',
        pageNo: 1,
        pageSize: 10
      },
      groupList: [],
      total: 0
    }
  },
  created () {
    this.getGroupList()
  },
  methods: {
    async getGroupList () {
      console.log(this.queryInfo.group_name)
      if (!this.queryInfo.group_name) {
        const { data: res } = await this.$http.post('http://127.0.0.1:8080/userprofile-platform/v1/UserGroup/usergroupList', this.queryInfo)
        this.groupList = res.data.list
        this.total = res.data.total
      } else {
        const { data: res } = await this.$http.post('http://127.0.0.1:8080/userprofile-platform/v1/UserGroup/searchUsergroup', {}, {
          params: {
            group_name: this.queryInfo.group_name
          }
        })
        console.log(res);
        this.groupList = res.data.list
        this.total = res.data.totalCount
      }
      console.log(this.total)
    },
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getGroupList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pageNo = newPage
      this.getGroupList()
    },
    handleAddGroup (groupInfo = null) {
      if (!groupInfo) {
        window.sessionStorage.clear()
        this.$router.push({ name: 'GroupAnalysis' })
      } else {
        this.$router.push({ name: 'GroupAnalysis', params: groupInfo })
      }
    },
    async removeGroupById (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该人群, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
      ).catch(err => err)
      // 如果确认删除返回值是 'confirm'
      // 取消了删除，则返回值为字符串 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除！')
      }
      const { data: res } = await this.$http.post('http://127.0.0.1:8080/userprofile-platform/v1/UserGroup/deleteUsergroup', {}, {
        params: {
          group_id: id
        }
      })
      this.$message.success('删除人群成功！')
      this.getGroupList()
    }
  }
}
</script>

<style scoped>
.el-card {
  padding: 5px;
}
.el-select {
  width: 100%;
}
</style>
